#include "task_template_factory.h"
#include "subtasks/move_task.h"
#include "subtasks/jack_task.h"
#include "subtasks/lift_task.h"
#include "subtasks/charge_task.h"
#include "subtasks/park_task.h"

namespace application {
namespace task {

TaskTemplateFactory::TaskTemplateFactory(
    std::shared_ptr<vehicle::VehicleService> vehicleService,
    std::shared_ptr<dispatcher::DispatcherService> dispatcherService,
    std::shared_ptr<MapApplicationService> mapService,
    std::shared_ptr<kernel::ILogger> logger)
    : m_vehicleService(vehicleService)
    , m_dispatcherService(dispatcherService)
    , m_mapService(mapService)
    , m_logger(logger)
{
}

std::vector<std::unique_ptr<SubTaskBase>> TaskTemplateFactory::createSubTasks(
    const kernel::task::TaskTemplateConfigDTO& templateConfig,
    int agvNumber)
{
    // 参考 task_manager.cpp:278-309

    if (m_logger) {
        m_logger->debug(QString("Creating subtasks for template: %1, agv: %2")
            .arg(QString::fromStdString(templateConfig.name))
            .arg(agvNumber));
    }

    std::vector<std::unique_ptr<SubTaskBase>> subTasks;

    for (const auto& subTaskConfig : templateConfig.subTasks) {
        auto subTask = createSubTask(subTaskConfig, agvNumber);

        if (!subTask) {
            if (m_logger) {
                m_logger->error(QString("Failed to create subtask for template: %1")
                    .arg(QString::fromStdString(templateConfig.name)));
            }
            return {};  // 返回空vector表示失败
        }

        subTasks.push_back(std::move(subTask));
    }

    if (m_logger) {
        m_logger->info(QString("Created %1 subtasks for template: %2")
            .arg(subTasks.size())
            .arg(QString::fromStdString(templateConfig.name)));
    }

    return subTasks;
}

std::unique_ptr<SubTaskBase> TaskTemplateFactory::createSubTask(
    const kernel::task::SubTaskConfigDTO& subTaskConfig,
    int agvNumber)
{
    switch (subTaskConfig.type) {
        case kernel::task::SubTaskType::Move:
            return createMoveTask(subTaskConfig, agvNumber);

        case kernel::task::SubTaskType::Jack:
            return createJackTask(subTaskConfig, agvNumber);

        case kernel::task::SubTaskType::Lift:
            return createLiftTask(subTaskConfig, agvNumber);

        case kernel::task::SubTaskType::Charge:
            return createChargeTask(subTaskConfig, agvNumber);

        case kernel::task::SubTaskType::Park:
            return createParkTask(subTaskConfig, agvNumber);

        default:
            if (m_logger) {
                m_logger->error("Unknown subtask type");
            }
            return nullptr;
    }
}

// ==================== 子任务创建方法 ====================

std::unique_ptr<SubTaskBase> TaskTemplateFactory::createMoveTask(
    const kernel::task::SubTaskConfigDTO& config,
    int agvNumber)
{
    // 参考 task_manager.cpp:282-287

    if (config.stationName.empty()) {
        if (m_logger) {
            m_logger->error("Move task: station_name is required");
        }
        return nullptr;
    }

    // 直接创建MoveTask，路径规划会在任务init阶段通过requestNavigation自动处理
    return std::make_unique<MoveTask>(
        config.stationName,
        config.floor,
        m_vehicleService,
        m_dispatcherService,
        m_mapService,
        m_logger,
        config.operation
    );
}

std::unique_ptr<SubTaskBase> TaskTemplateFactory::createJackTask(
    const kernel::task::SubTaskConfigDTO& config,
    int agvNumber)
{
    // 参考 task_manager.cpp:289-293

    return std::make_unique<JackTask>(
        config.isUp,
        m_vehicleService,
        m_logger,
        config.operationStation,
        config.waitTimeSeconds
    );
}

std::unique_ptr<SubTaskBase> TaskTemplateFactory::createLiftTask(
    const kernel::task::SubTaskConfigDTO& config,
    int agvNumber)
{
    // 参考 task_manager.cpp:295-304

    if (config.liftNum <= 0 || config.callFloor <= 0 || config.aimFloor <= 0) {
        if (m_logger) {
            m_logger->error("Lift task: invalid parameters");
        }
        return nullptr;
    }

    return std::make_unique<LiftTask>(
        m_vehicleService,
        m_mapService,
        m_dispatcherService,
        m_logger,
        config.liftNum,
        config.callFloor,
        config.aimFloor
    );
}

std::unique_ptr<SubTaskBase> TaskTemplateFactory::createChargeTask(
    const kernel::task::SubTaskConfigDTO& config,
    int agvNumber)
{
    return std::make_unique<ChargeTask>(
        m_vehicleService,
        m_mapService,
        m_dispatcherService,
        m_logger,
        config.chargePoint,
        config.targetBatteryLevel
    );
}

std::unique_ptr<SubTaskBase> TaskTemplateFactory::createParkTask(
    const kernel::task::SubTaskConfigDTO& config,
    int agvNumber)
{
    // 参考 task_manager.cpp:305-308

    return std::make_unique<ParkTask>(
        m_vehicleService,
        m_mapService,
        m_dispatcherService,
        m_logger,
        config.parkingStation.empty() ? "" : config.parkingStation,
        config.floor
    );
}

} // namespace task
} // namespace application
